<?php

namespace app\admin\controller\xiservice;

use app\common\controller\Backend;
use app\common\model\xiservice\Timelist;
use app\common\model\xiservice\User;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Schedule extends Backend
{

    /**
     * Schedule模型对象
     * @var \app\common\model\xiservice\Schedule
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\xiservice\Schedule;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','date','rest_start_time']);
                
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /*
    ** 服务人员排班
     */
    public function worker($ids=null)
    {

        if ($this->request->isAjax())
        {
            $date = $this->request->request('date');
            $date = $date?$date:date("Y-m-d");
             
            $list = $this->model
                        ->where('user_id',$ids)
                        ->where('date',$date)
                        ->select();

            $worker =   User::where('id',$ids)->find();
            if(!$worker){
                $result=["code"=>0,"data"=>[],"msg"=>"服务人员不存在!"];
                return json($result);
            } 
            if(!$worker['work_start_time']){
                $result=["code"=>0,"data"=>[],"msg"=>"暂未设置工作时间!"];
                return json($result);
            }                    
            $Timelist=Timelist::where('start_time','>=',$worker['work_start_time'])->where('end_time','<=',$worker['work_end_time'])->order('id asc')->select();
            $time = time();
            foreach ($Timelist as $row) {
                $row['checked'] = 1;
                $row['user_id'] = $ids;
                $row['date'] = $date;
                $timestamp = strtotime($date." ".$row['start_time']);
                if( $timestamp < $time){
                         $row['checked'] = 3;
                }
                if($list){
                    foreach ($list as $rows) {
                        
                        if($row['start_time']==$rows['rest_start_time']){
                            if($rows['type']==2){
                                $row['checked'] = 2;
                            }else{
                                $row['checked'] = 0;
                            }
                        }
                    }
                }

            }
            $result=["code"=>1,"data"=>$Timelist,"msg"=>"查询成功"];
            return json($result);
        }

        $this->view->assign("id", $ids);
        return $this->view->fetch();        

    }

    /*
    ** 修改工作时间
     */
    public function edit_status()
    {
         if ($this->request->isAjax())
        {  

            $id = $this->request->request('id');
            $user_id = $this->request->request('user_id');
            $date = $this->request->request('date');
            $act = $this->request->request('act'); 
            //act = 0 休息  1= 工作
            $Timelist=Timelist::where('id',$id)->find();
            
            //判断当前时间是否合适修改
            $time = strtotime($date." ".$Timelist['start_time']);
            $nowtime = time();
            if($time < $nowtime){
                 $result=["code"=>0,"data"=>[],"msg"=>"当前时间不可选!"];
                return json($result);
            }

            $check = $this->model->where('user_id',$user_id)->where('date',$date)->where('rest_start_time',$Timelist['start_time'])->find();
            if($check){
                 if($check['type']==2){
                    $result=["code"=>0,"data"=>[],"msg"=>"当前时间已被预约不能修改!"];
                    return json($result);
                 }
            }
            if($act==0){
                    $data['user_id']=$user_id;
                    $data['date']=$date;
                    $data['rest_start_time'] = $Timelist['start_time'];
                    $data['rest_timestamp'] = strtotime($date." ".$Timelist['start_time']);
                    $res = $this->model->insertGetId($data);
                    if($res){
                        $result=["code"=>1,"data"=>[],"msg"=>"操作成功"];
                    }else{
                        $result=["code"=>0,"data"=>[],"msg"=>"操作失败,请重试!"];
                    }
            }else{

                    $res = $this->model->where('user_id',$user_id)->where('date',$date)->where('rest_start_time',$Timelist['start_time'])->delete();
                    $result=["code"=>1,"data"=>[],"msg"=>"操作成功"];
            }

            return json($result);
        }

        return ["code"=>0,"data"=>[],"msg"=>"参数错误"];
    }

}
